﻿using System;
using System.Collections;
using System.Configuration;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Collections.Generic;
using System.IO;
using BUS;
using DTO;
using System.Text;

namespace Cucre
{
    public partial class WebForm17 : System.Web.UI.Page
    {
        DataClasses1DataContext Data = new DataClasses1DataContext();
        private string city;
        int LoaiTinKhuyenMai;
        protected override void OnPreInit(EventArgs e)
        {
            base.OnPreInit(e);
            string path = Server.MapPath("theme.txt");
            StreamReader SR = new StreamReader(path);
            string nameTheme = SR.ReadLine();
            SR.Close();
            base.OnPreInit(e);
            if (Session["MyTheme"] == null)
            {
                Session.Add("MyTheme", nameTheme);
                Page.Theme = (string)Session["MyTheme"];
            }
            else
            {
                Page.Theme = (string)Session["MyTheme"];
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            city = DropDownList1.SelectedValue.Trim();
            LoaiTinKhuyenMai = LoaiTinKMDropDownList.SelectedIndex;
            if (TinKM_Gridview.SelectedIndex < 0)
                TenSanPhamLabel1.Text = TenSanPhamLabel2.Text = "Bạn Chưa Chọn Sản Phẩm (Tin Khuyến Mãi) nào !!!!";

            if (!IsPostBack)
            {
                TinKhuyenMaiDataBind();

            }
            ShowComment();
        }

       
        public void TinKhuyenMaiDataBind()
        {

            switch (LoaiTinKhuyenMai)
            {
                case 0:// dang khuyen mai
                    {
                        TinKM_Gridview.DataSource = from product in Data.PRODUCTs
                                    where ((product.STATUS.Value == 1) && (product.CITY.Trim() == city.Trim()) && (product.VISIBLE == true)
                                        && ((product.TIME_START.Value - DateTime.Today).TotalSeconds < 0)
                                        && ((product.TIME_STOP.Value - DateTime.Today).TotalSeconds > 0))
                                    select new
                                    {
                                        IDPRODUCT = product.IDPRODUCT,
                                        PROVIDER_NAME = product.PROVIDER.COMPANY_NAME,
                                        Name_Product = product.Name_Product,
                                        TIME_START = product.TIME_START.Value.ToShortDateString(),
                                        TIME_STOP = product.TIME_STOP.Value.ToShortDateString(),
                                        PRICE = product.PRICE,
                                        CITY = product.CITY
                                    };
                       
                            TinKM_Gridview.DataBind();
                            DanhSachComment.Controls.Clear();
                            TinKM_Gridview.SelectedIndex = -1;
                    }
                    break;
                case 1:// sap khuyen mai
                    {
                        TinKM_Gridview.DataSource = from product in Data.PRODUCTs
                                    where (((product.TIME_START.Value - DateTime.Today).TotalSeconds > 0) && (product.CITY.Trim() == city.Trim())
                                        && (product.STATUS.Value == 1) && (product.VISIBLE == true))
                                    select new
                                    {
                                        IDPRODUCT = product.IDPRODUCT,
                                        PROVIDER_NAME = product.PROVIDER.COMPANY_NAME,
                                        Name_Product = product.Name_Product,
                                        TIME_START = product.TIME_START.Value.ToShortDateString(),
                                        TIME_STOP = product.TIME_STOP.Value.ToShortDateString(),
                                        PRICE = product.PRICE,
                                        CITY = product.CITY
                                    };
                        TinKM_Gridview.DataBind();
                        DanhSachComment.Controls.Clear();
                        TinKM_Gridview.SelectedIndex = -1;
                    }
                    break;
                case 2:// da ket thuc
                    {
                        TinKM_Gridview.DataSource = from product in Data.PRODUCTs
                                    where (((product.TIME_STOP.Value - DateTime.Today).TotalSeconds < 0) &&
                                          (product.CITY.Trim() == city.Trim()) && (product.STATUS.Value == 1) && (product.VISIBLE == true))
                                    select new
                                    {
                                        IDPRODUCT = product.IDPRODUCT,
                                        PROVIDER_NAME = product.PROVIDER.COMPANY_NAME,
                                        Name_Product = product.Name_Product,
                                        TIME_START = product.TIME_START.Value.ToShortDateString(),
                                        TIME_STOP = product.TIME_STOP.Value.ToShortDateString(),
                                        PRICE = product.PRICE,
                                        CITY = product.CITY
                                    };
                        TinKM_Gridview.DataBind();
                                    DanhSachComment.Controls.Clear();
                                    TinKM_Gridview.SelectedIndex = -1;
                    }
                    break;
            }
           
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            city = DropDownList1.SelectedValue;
            TinKhuyenMaiDataBind();
        }

        protected void LoaiTinKMDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoaiTinKhuyenMai = LoaiTinKMDropDownList.SelectedIndex;
            TinKhuyenMaiDataBind();
        }

        protected void TinKM_Gridview_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {          
            TinKM_Gridview.SelectedIndex = e.NewSelectedIndex;
            ShowComment();
        }

        public void ShowComment()
        {
            if (TinKM_Gridview == null)
                return;
            if (TinKM_Gridview.SelectedIndex >= 0)
            {
                TenSanPhamLabel1.Text = TenSanPhamLabel2.Text = TinKM_Gridview.Rows[TinKM_Gridview.SelectedIndex].Cells[2].Text;
                int ID = int.Parse(TinKM_Gridview.Rows[TinKM_Gridview.SelectedIndex].Cells[0].Text);
                List<COMMENT> ListComment = new List<COMMENT>();
                ListComment = CommentBUS.GetComment(ID);
                DanhSachComment.Controls.Clear();
                if (ListComment == null)
                    return;
                for (int i = 0; i < ListComment.Count; ++i)
                {
                    Label la = new Label();
                    la.Text = "<div style='height:20px' align='left'>" + "         Được Gửi Bởi : " + ListComment[i].ACCOUNT.USERNAME + "<br/>         Vào lúc : " + ListComment[i].TIME_COMMENT.Value.ToString() + "</div><br/>";
                    DanhSachComment.Controls.Add(la);
                    HtmlTextArea h = new HtmlTextArea();
                    h.Value = ListComment[i].CONTENT;
                    h.ID = ListComment[i].IDCOMMENT.ToString();
                    h.Style.Value = "width:600px";
                    DanhSachComment.Controls.Add(h);
                    //<asp:LinkButton OnClientClick="Lưu Lại Những Thay Đổi" ID="capnhat" runat="server" Text="taday" Font-Bold="true" Font-Underline="true" ForeColor="Blue"></asp:LinkButton>
                    LinkButton Linkbt = new LinkButton();
                    LinkButton LinkDeletebt = new LinkButton();
                    Linkbt.ID = "LinkButton" + ListComment[i].IDCOMMENT.ToString();
                    LinkDeletebt.ID = "LinkDeleteButton" + ListComment[i].IDCOMMENT.ToString();
                    Linkbt.Text = " <div style='width:550px' align='right'> Save Change   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;  ";
                    LinkDeletebt.Text = "     Delete </div><br/><br/> ";

                    Linkbt.Font.Bold = LinkDeletebt.Font.Bold = true;
                    // Linkbt.Font.Size = new FontUnit(FontSize.NotSet);
                    Linkbt.ForeColor = LinkDeletebt.ForeColor = Color.Blue;
                    // Linkbt.Font.Underline = true;//LinkDeletebt.Font.Underline= true;

                    Linkbt.Click += new EventHandler(Linkbt_Click);

                    LinkDeletebt.Click += new EventHandler(LinkDeletebt_Click);
                    Linkbt.OnClientClick = "return confirm('Bạn Có Chắc Chắn Muốn Thay Đổi Nội Dung Comment Này?')";
                    LinkDeletebt.OnClientClick = "return confirm('Bạn Có Chắc Chắn Muốn Xóa Comment Này?')";
                    DanhSachComment.Controls.Add(Linkbt);
                    DanhSachComment.Controls.Add(LinkDeletebt);
                }
            }
            else
            {
                DanhSachComment.Text = "";
            }
        }
        void LinkDeletebt_Click(object sender, EventArgs e)
        {
            try
            {
                LinkButton linkbutton = (LinkButton)sender;
                string stringID = linkbutton.ID.Remove(0, 16);
                int ID = int.Parse(stringID);
                bool Result=CommentBUS.Delete(ID);
                if (Result)
                    ShowComment();
                else
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "clientScript", "alert('Qúa Trình Thực Hiện Thất Bại. Vui Lòng Kiểm tra lại')", true);
                }
            }
            catch (Exception ex)
            {
                string message = "alert('" + ex.Message + "')";
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "clientScript", message, true);
            }
        }

        void Linkbt_Click(object sender, EventArgs e)
        {
            try
            {
                LinkButton linkbutton = (LinkButton)sender;
                string stringID = linkbutton.ID.Remove(0, 10);
                int ID = int.Parse(stringID);
                COMMENT Comment = new COMMENT();
                Comment.IDCOMMENT = ID;
                Comment.CONTENT = ((HtmlTextArea)DanhSachComment.FindControl(ID.ToString())).Value;
                CommentBUS.Update(Comment);
            }
            catch (Exception ex)
            {
                string message = "alert('" + ex.Message + "')";
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "clientScript", message, true);
            }
        }

        protected void TinKM_Gridview_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            TinKM_Gridview.PageIndex = e.NewPageIndex;
            TinKhuyenMaiDataBind();
        }

        protected void AddComment_bt_Click(object sender, EventArgs e)
        {
            try
            {
                if (TinKM_Gridview.SelectedIndex >= 0)
                {

                    string content = AddComment_Textarea.Value.Trim();
                    if (content == "")
                    {
                        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "clientScript", "alert('Bạn Chưa Nhập Nội Dung Comment vào !!!. Vui Lòng Kiểm Tra lại')", true);
                        return;
                    }
                    int IDProduct = int.Parse(TinKM_Gridview.Rows[TinKM_Gridview.SelectedIndex].Cells[0].Text);
                    MEMBER member = (MEMBER)Session["user_login"];
                    COMMENT Comment = new COMMENT();
                    Comment.IDACCOUNT = member.IDACCOUNT;
                    //Comment.IDACCOUNT = 8;
                    Comment.IDPRODUCT = IDProduct;
                    Comment.CONTENT = content;
                    CommentBUS.Insert(Comment);
                    ShowComment();
                    AddComment_Textarea.Value = "";
                }
                else
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "clientScript", "alert('Bạn Chưa Chọn Tin Khuyến Mãi Nào !!!. Vui Lòng Kiểm Tra lại')", true);
                }
            }
            catch (Exception ex)
            {
                string message = "alert('" + ex.Message + "')";
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "clientScript", message, true);
            }
        }
    }
}
    